﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KBManagement.BIZ;
using KBManagement.DAL;
using KBManagement.Library;

namespace KBManagement
{
    public partial class AddEditComponent : Form,InforObserver
    {

        private Food food;
        StoreTempFacade storeTempFacade;
        FoodFacade foodFacade;
        KBSQLClassDataContext context;
        IQueryable sTemp;
        int componentIndex = 0;
        public AddEditComponent(Food food)
        {
            foodFacade = new FoodFacade();
            Utils.CreateInstance().AddInforObserver(this);
            context = Utils.CreateInstance().GetDBContext();
            storeTempFacade = new StoreTempFacade();
            this.food = food;
            InitializeComponent();
            try
            {
                BindingComboBox();
                SelectComponent();
            }
            catch (Exception)
            { }
            lblNameFood.Text = food.Fo_Name;
        }

        public void BindingComboBox()
        {
            sTemp = storeTempFacade.Select();
            comboBox1.DataSource = sTemp;
            comboBox1.DisplayMember = "S_Name";
            comboBox1.ValueMember = "ID";
            comboBox1.SelectedIndex = 0;
            AddUnit(comboBox1.SelectedValue.ToString());
        }

        public void SelectComponent()
        {
            var compo = from fc in Utils.CreateInstance().GetDBContext().FoodComponents where fc.FoId == food.ID
                        select new
                        {
                            fc.ComId,
                            fc.StoreTemp.Store.S_Name,
                            fc.ComQuantilty,
                            fc.StoreTemp.Store.Unit
                        };
            dataGridView1.DataSource = compo;
            dataGridView1.Columns[0].HeaderText = "Mã số";
            dataGridView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
            dataGridView1.Columns[1].HeaderText = "Tên";
            dataGridView1.Columns[2].HeaderText = "Số lượng";
            dataGridView1.Columns[3].HeaderText = "Đơn vị";
            try
            {
                dataGridView1.CurrentCell = dataGridView1[0, componentIndex];
            }
            catch (Exception)
            {

            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex <0)
            {
                MessageBox.Show("Xin mời lựa chọn 1 thành phần");
                return;
            }

            if (Utils.CreateInstance().isNull(textBox1.Text))
            {
                MessageBox.Show("Xin mời nhập vào số lượng");
                return;
            }

            if (!Utils.CreateInstance().isNumberDouble(textBox1.Text))
            {
                MessageBox.Show("Số lượng không đúng định dạng");
                return;
            }

            FoodComponent fComponent = Utils.CreateInstance().GetDBContext().FoodComponents.SingleOrDefault(x => x.StoreTempId == comboBox1.SelectedValue.ToString() && x.FoId == food.ID);
                if (fComponent == null)
                {
                    FoodComponent compo = new FoodComponent
                    {
                        StoreTempId = comboBox1.SelectedValue.ToString(),
                        ComQuantilty = Convert.ToDouble(textBox1.Text),
                        FoId = food.ID
                    };
                    food.FoodComponents.Add(compo);
                }
                else
                {
                    fComponent.ComQuantilty = fComponent.ComQuantilty + Convert.ToDouble(textBox1.Text);
                }
                context.SubmitChanges();
                Utils.CreateInstanceLog().TransactionLogCommon("Nhân viên " + AdminSession.admin.UserName + " thêm thành phần " + comboBox1.SelectedText.ToString() + " vào " + lblNameFood.Text + " lúc " + DateTime.Now);
                SelectComponent();
                textBox1.Text = "";
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            AddUnit(comboBox1.SelectedValue.ToString());
        }

        private void AddUnit(string id)
        {
            var storeTemp = from fc in Library.Utils.CreateInstance().GetDBContext().StoreTemps where fc.ID == id select fc;

            foreach (StoreTemp sTemp in storeTemp)
            {
                lblUnit.Text = sTemp.Store.Unit.ToString();
                break;
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            toolStripButton1.Enabled = true;
            toolStripButton2.Enabled = true;
            componentIndex = e.RowIndex;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            FoodComponent comp = context.FoodComponents.SingleOrDefault(x => x.ComId == Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value.ToString()));
            ConfirmDelete confirm = new ConfirmDelete(comp);
            if (confirm .ShowDialog()== DialogResult.OK)
            {
                context.FoodComponents.DeleteOnSubmit(comp);
                context.SubmitChanges();
                SelectComponent();
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            FoodComponent comp = context.FoodComponents.SingleOrDefault(x => x.ComId == Convert.ToInt32(dataGridView1.SelectedRows[0].Cells[0].Value.ToString()));
            new EditQtityComponent(comp).ShowDialog();
        }

        public void Update()
        {
            SelectComponent();
        }
    }
}
